package com.ycsj.goldmedalnewconcept.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.WindowManager;

/**
 * 创建日期：2017/8/13  12:31
 * 描述:自定义的虚拟状态栏
 * 作者:张伟
 */

public class StatusBarView extends View {

    private Context context;

    public StatusBarView(Context context) {
        super(context);
        this.context = context;
    }

    public StatusBarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    public StatusBarView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //设置宽度为整个屏幕的宽度，高度为状态栏高度
        setMeasuredDimension(getWindowWidth(context), getStatusBarHeight(context));
    }

    /**
     * 获取屏幕宽度
     *
     * @param context
     * @return
     */
    public static int getWindowWidth(Context context) {
        int w = 0;
        try {
            WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            w = wm.getDefaultDisplay().getWidth();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return w;
    }

    /**
     * 获取状态栏高度
     *
     * @param context
     * @return
     */
    public static int getStatusBarHeight(Context context) {

        int result = 0;
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = context.getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

}
